<template>
  <div class="login-container">
    <div class="login-box">
      <img class="login-banner" src="@/assets/images/login-banner.png">
      <div class="welcome">
        <p class="title">欢迎登录</p>
        <p class="content">义卡通管理后台</p>
      </div>
      <div class="login-form">
        <p class="login-title">义卡通管理后台</p>
        <el-tabs v-model="loginType" class="login-tabs" @tab-click="changeLoginType">
          <el-tab-pane label="账号登录" name="1" />
          <el-tab-pane label="手机号登录" name="2" />
        </el-tabs>
        <el-form
          v-if="loginType === '1'"
          :key="loginType"
          ref="accountForm"
          :model="accountForm"
          :rules="accountRules"
          label-width="80px"
          label-position="left"
        >
          <el-form-item label="账号：" prop="account">
            <el-input
              v-model="accountForm.account"
              class="form-input"
              placeholder="请输入账号"
              type="text"
              tabindex="1"
            />
          </el-form-item>

          <el-form-item label="密码：" prop="password">
            <el-input
              v-model="accountForm.password"
              class="form-input"
              type="password"
              placeholder="请输入密码"
              tabindex="2"
            />
          </el-form-item>
          <el-button :loading="loading" class="login-btn" type="primary" @click.native.prevent="handleLogin">登录
          </el-button>
        </el-form>
        <el-form
          v-if="loginType === '2'"
          :key="loginType"
          ref="phoneForm"
          :model="phoneForm"
          :rules="phoneRules"
          label-width="80px"
          label-position="left"
        >
          <el-form-item label="手机号：" prop="phone">
            <el-input
              v-model="phoneForm.phone"
              class="form-input"
              placeholder="请输入手机号"
              type="text"
              tabindex="1"
              maxlength="11"
            />
          </el-form-item>

          <el-form-item label="验证码：" prop="code">
            <el-input
              v-model="phoneForm.password"
              class="form-input"
              type="text"
              placeholder="请输入验证码"
              tabindex="2"
            >
              <span v-if="showTime" slot="append" class="send-code">{{ durationTime }}s</span>
              <el-button v-else slot="append" class="send-code" @click="getCode">获取验证码</el-button>
            </el-input>

          </el-form-item>
          <el-button :loading="loading" class="login-btn" type="primary" @click.native.prevent="handleLogin">登录
          </el-button>
        </el-form>
      </div>
    </div>
    <img class="login-bg" src="@/assets/images/login-bg.png">
  </div>
</template>
<script>

import { accountLogin } from '@/api/login'
export default {
  name: 'Login',
  data() {
    // const validatePassword = (rule, value, callback) => {
    //   if (value.length < 6) {
    //     callback(new Error('The password can not be less than 6 digits'))
    //   } else {
    //     callback()
    //   }
    // }
    return {
      loginType: '1',
      accountForm: {
        account: 'zhou',
        password: '123456'
      },
      phoneForm: {
        phone: '',
        code: ''
      },
      accountRules: {
        account: [{ required: true, trigger: 'blur', message: '请输入账号' }],
        password: [{ required: true, trigger: 'blur', message: '请输入密码' }]
      },
      phoneRules: {
        phone: [{ required: true, trigger: 'blur', message: '请输入手机号' }],
        code: [{ required: true, trigger: 'blur', message: '请输入验证码' }]
      },
      timer: null,
      showTime: false,
      durationTime: 59,
      loading: false,
      redirect: ''
    }
  },
  watch: {
    $route: {
      handler: function(route) {
        this.redirect = route.query && route.query.redirect
      },
      immediate: true
    }
  },
  methods: {

    // 切换登录方式
    changeLoginType() {
      clearInterval(this.timer)
      this.showTime = false
      this.durationTime = 59
    },

    // 获取验证码
    getCode() {
      this.timeStart()
    },

    // 倒计时
    timeStart() {
      this.showTime = true
      this.timer = setInterval(() => {
        while (this.durationTime === 1) {
          this.showTime = false
          this.durationTime = 59
          clearInterval(this.timer)
          return
        }
        this.durationTime--
      }, 1000)
    },

    // 登录
    async handleLogin() {
      let $form
      let data = {}
      let loginApi
      if (this.loginType === '1') {
        $form = this.$refs.accountForm
        data = this.accountForm
        loginApi = accountLogin
      } else {
        $form = this.$refs.mobileForm
        data = this.mobileForm
        // todo 手机号登录
        // loginApi = mobileLogin
      }
      $form.validate(async(valid) => {
        if (valid) {
          try {
            this.loading = true
            await this.$store.dispatch('user/login', { data, loginApi })
            await this.$router.push({ path: this.redirect || '/' })
            this.loading = false
          } catch (e) {
            this.loading = false
          }
        } else {
          return false
        }
      })
    }
  }
}
</script>

<style lang="scss" scoped>
@import "@/styles/variables.scss";
.login-container {
  position: relative;
  width: 100vw;
  height: 100vh;
  overflow: hidden;
  background: $bg-color ;

  .login-box{
    position: absolute;
    height: 387px;
    display: flex;
    align-items: center;
    justify-content: left;
    border-radius: 0 8px 8px 0;
    top: 50%;
    right: 50px;
    transform: translateY(-50%);
    overflow: hidden;
    z-index: 10;
  }
  .login-banner{
    width: 450px;
    height: auto;
  }

  .welcome{
    position: absolute;
    top:100px;
    left: 50px;
    color: #fff;
    .title{
      font-size: 16px;
      margin-bottom: 10px;
    }
    .content{
      font-size: 24px;
    }
  }

  .login-form {
    width: 470px;
    height: 100%;
    background: #fff;
    box-shadow: 0 0 20px 0 rgba(0, 13, 88, .1);
    padding: 30px;

    .login-title {
      font-size: 28px;
      color: $primary;
      margin-bottom: 20px;
      text-align: center;
      font-weight: bold;
    }

    .login-tabs{
      margin-bottom: 20px;
    }

    .form-input{
      width: 100%;
      margin-bottom: 10px;
    }

    .send-code{
      color: $primary;
    }

    ::v-deep {
      .el-input__inner,.el-form-item__label{
        height: 40px;
        line-height: 40px;
      }
    }

    .login-btn {
      display: inline-block;
      width: 100%;
      height: 40px;
      font-size: 18px;
      margin-top: 15px;
    }
  }

  .login-bg{
    position: absolute;
    width: 100vw;
    bottom: 0;
    left: 0;
    z-index: 1;
  }
}
</style>
